Subject: Test Content-Length header parsing
Origin: v2.9.4-7-g4b99e42 <https://github.com/joyent/http-parser/commit/4b99e42>
Upstream-Author: Ben Noordhuis <info@bnoordhuis.nl>
Date: Wed Jul 8 02:30:29 2020 +0200

    The test suite did very little validation of the Content-Length field
    until now. Verify for each request and response that the parsed numeric
    value matches the value from the header field.

    PR-URL: https://github.com/nodejs/http-parser/pull/519
    Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>

--- a/test.c
+++ b/test.c
@@ -74,6 +74,7 @@
 
   unsigned short http_major;
   unsigned short http_minor;
+  uint64_t content_length;
 
   int message_begin_cb_called;
   int headers_complete_cb_called;
@@ -108,6 +109,7 @@
   ,.fragment= ""
   ,.request_path= "/test"
   ,.request_url= "/test"
+  ,.content_length= -1
   ,.num_headers= 3
   ,.headers=
     { { "User-Agent", "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1" }
@@ -139,6 +141,7 @@
   ,.fragment= ""
   ,.request_path= "/favicon.ico"
   ,.request_url= "/favicon.ico"
+  ,.content_length= -1
   ,.num_headers= 8
   ,.headers=
     { { "Host", "0.0.0.0=5000" }
@@ -168,6 +171,7 @@
   ,.fragment= ""
   ,.request_path= "/dumbluck"
   ,.request_url= "/dumbluck"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "aaaaaaaaaaaaa",  "++++++++++" }
@@ -190,6 +194,7 @@
   ,.request_path= "/forums/1/topics/2375"
   /* XXX request url does include fragment? */
   ,.request_url= "/forums/1/topics/2375?page=1#posts-17408"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.body= ""
   }
@@ -208,6 +213,7 @@
   ,.fragment= ""
   ,.request_path= "/get_no_headers_no_body/world"
   ,.request_url= "/get_no_headers_no_body/world"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.body= ""
   }
@@ -227,6 +233,7 @@
   ,.fragment= ""
   ,.request_path= "/get_one_header_no_body"
   ,.request_url= "/get_one_header_no_body"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Accept" , "*/*" }
@@ -250,6 +257,7 @@
   ,.fragment= ""
   ,.request_path= "/get_funky_content_length_body_hello"
   ,.request_url= "/get_funky_content_length_body_hello"
+  ,.content_length= 5
   ,.num_headers= 1
   ,.headers=
     { { "conTENT-Length" , "5" }
@@ -274,6 +282,7 @@
   ,.fragment= "hey"
   ,.request_path= "/post_identity_body_world"
   ,.request_url= "/post_identity_body_world?q=search#hey"
+  ,.content_length= 5
   ,.num_headers= 2
   ,.headers=
     { { "Accept", "*/*" }
@@ -300,6 +309,7 @@
   ,.fragment= ""
   ,.request_path= "/post_chunked_all_your_base"
   ,.request_url= "/post_chunked_all_your_base"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Transfer-Encoding" , "chunked" }
@@ -328,6 +338,7 @@
   ,.fragment= ""
   ,.request_path= "/two_chunks_mult_zero_end"
   ,.request_url= "/two_chunks_mult_zero_end"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Transfer-Encoding", "chunked" }
@@ -358,6 +369,7 @@
   ,.fragment= ""
   ,.request_path= "/chunked_w_trailing_headers"
   ,.request_url= "/chunked_w_trailing_headers"
+  ,.content_length= -1
   ,.num_headers= 3
   ,.headers=
     { { "Transfer-Encoding",  "chunked" }
@@ -388,6 +400,7 @@
   ,.fragment= ""
   ,.request_path= "/chunked_w_nonsense_after_length"
   ,.request_url= "/chunked_w_nonsense_after_length"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Transfer-Encoding", "chunked" }
@@ -410,6 +423,7 @@
   ,.fragment= ""
   ,.request_path= "/with_\"stupid\"_quotes"
   ,.request_url= "/with_\"stupid\"_quotes?foo=\"bar\""
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= { }
   ,.body= ""
@@ -436,6 +450,7 @@
   ,.fragment= ""
   ,.request_path= "/test"
   ,.request_url= "/test"
+  ,.content_length= -1
   ,.num_headers= 3
   ,.headers= { { "Host", "0.0.0.0:5000" }
              , { "User-Agent", "ApacheBench/2.3" }
@@ -459,6 +474,7 @@
   ,.fragment= ""
   ,.request_path= "/test.cgi"
   ,.request_url= "/test.cgi?foo=bar?baz"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= {}
   ,.body= ""
@@ -480,6 +496,7 @@
   ,.fragment= ""
   ,.request_path= "/test"
   ,.request_url= "/test"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= { }
   ,.body= ""
@@ -507,6 +524,7 @@
   ,.fragment= ""
   ,.request_path= "/demo"
   ,.request_url= "/demo"
+  ,.content_length= -1
   ,.num_headers= 7
   ,.upgrade="Hot diggity dogg"
   ,.headers= { { "Host", "example.com" }
@@ -538,6 +556,7 @@
   ,.fragment= ""
   ,.request_path= ""
   ,.request_url= "0-home0.netscape.com:443"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.upgrade="some data\r\nand yet even more data"
   ,.headers= { { "User-agent", "Mozilla/1.1N" }
@@ -560,6 +579,7 @@
   ,.fragment= ""
   ,.request_path= "/test"
   ,.request_url= "/test"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= {}
   ,.body= ""
@@ -579,6 +599,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= {}
   ,.body= ""
@@ -601,6 +622,7 @@
   ,.fragment= ""
   ,.request_path= "*"
   ,.request_url= "*"
+  ,.content_length= -1
   ,.num_headers= 3
   ,.headers= { { "HOST", "239.255.255.250:1900" }
              , { "MAN", "\"ssdp:discover\"" }
@@ -636,6 +658,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= -1
   ,.num_headers= 5
   ,.headers= { { "Line1", "abc\tdef ghi\t\tjkl  mno \t \tqrs" }
              , { "Line2", "line2\t" }
@@ -662,6 +685,7 @@
   ,.request_path= ""
   ,.request_url= "http://hypnotoad.org?hail=all"
   ,.host= "hypnotoad.org"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= { }
   ,.body= ""
@@ -683,6 +707,7 @@
   ,.request_url= "http://hypnotoad.org:1234?hail=all"
   ,.host= "hypnotoad.org"
   ,.port= 1234
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= { }
   ,.body= ""
@@ -704,6 +729,7 @@
   ,.request_url= "http://hypnotoad.org:1234"
   ,.host= "hypnotoad.org"
   ,.port= 1234
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= { }
   ,.body= ""
@@ -728,6 +754,7 @@
   ,.fragment= ""
   ,.request_path= "/file.txt"
   ,.request_url= "/file.txt"
+  ,.content_length= 10
   ,.num_headers= 4
   ,.headers= { { "Host", "www.example.com" }
              , { "Content-Type", "application/example" }
@@ -753,6 +780,7 @@
   ,.fragment= ""
   ,.request_path= ""
   ,.request_url= "HOME0.NETSCAPE.COM:443"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.upgrade=""
   ,.headers= { { "User-agent", "Mozilla/1.1N" }
@@ -777,6 +805,7 @@
   ,.fragment= "narf"
   ,.request_path= "/δ¶/δt/pope"
   ,.request_url= "/δ¶/δt/pope?q=1#narf"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers= { {"Host", "github.com" }
              }
@@ -799,6 +828,7 @@
   ,.fragment= ""
   ,.request_path= ""
   ,.request_url= "home_0.netscape.com:443"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.upgrade=""
   ,.headers= { { "User-agent", "Mozilla/1.1N" }
@@ -826,6 +856,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= 4
   ,.num_headers= 3
   ,.upgrade= 0
   ,.headers= { { "Host", "www.example.com" }
@@ -854,6 +885,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= 4
   ,.num_headers= 4
   ,.upgrade= 0
   ,.headers= { { "Host", "www.example.com" }
@@ -879,6 +911,7 @@
   ,.fragment= ""
   ,.request_path= "/file.txt"
   ,.request_url= "/file.txt"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers= { { "Host", "www.example.com" } }
   ,.body= ""
@@ -899,6 +932,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers= { { "Host", "www.example.com" } }
   ,.body= ""
@@ -920,6 +954,7 @@
   ,.host= "hypnotoad.org"
   ,.userinfo= "a%12:b!&*$"
   ,.port= 1234
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= { }
   ,.body= ""
@@ -952,6 +987,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= -1
   ,.num_headers= 5
   ,.headers= { { "Line1", "abc\tdef ghi\t\tjkl  mno \t \tqrs" }
              , { "Line2", "line2\t" }
@@ -985,6 +1021,7 @@
   ,.fragment= ""
   ,.request_path= "/demo"
   ,.request_url= "/demo"
+  ,.content_length= -1
   ,.num_headers= 7
   ,.upgrade="Hot diggity dogg"
   ,.headers= { { "Host", "example.com" }
@@ -1015,6 +1052,7 @@
   ,.fragment= ""
   ,.request_path= "/demo"
   ,.request_url= "/demo"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.upgrade="Hot diggity dogg"
   ,.headers= { { "Connection", "keep-alive, upgrade" }
@@ -1040,6 +1078,7 @@
   ,.fragment= ""
   ,.request_path= "/demo"
   ,.request_url= "/demo"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.upgrade="Hot diggity dogg"
   ,.headers= { { "Connection", "keep-alive,  upgrade" }
@@ -1066,6 +1105,7 @@
   ,.method= HTTP_POST
   ,.request_path= "/demo"
   ,.request_url= "/demo"
+  ,.content_length= 15
   ,.num_headers= 4
   ,.upgrade="Hot diggity dogg"
   ,.headers= { { "Host", "example.com" }
@@ -1091,6 +1131,7 @@
   ,.http_minor= 0
   ,.method= HTTP_CONNECT
   ,.request_url= "foo.bar.com:443"
+  ,.content_length= 10
   ,.num_headers= 3
   ,.upgrade="blarfcicle"
   ,.headers= { { "User-agent", "Mozilla/1.1N" }
@@ -1121,6 +1162,7 @@
   ,.request_url= "/images/my_dog.jpg"
   ,.query_string= ""
   ,.fragment= ""
+  ,.content_length= -1
   ,.num_headers= 3
   ,.headers= { { "Host", "example.com" }
              , { "Link", "<http://example.com/profiles/joe>; rel=\"tag\"" }
@@ -1145,6 +1187,7 @@
   ,.request_url= "/images/my_dog.jpg"
   ,.query_string= ""
   ,.fragment= ""
+  ,.content_length= -1
   ,.num_headers= 2
   ,.headers= { { "Host", "example.com" }
 	     , { "Link", "<http://example.com/profiles/sally>; rel=\"tag\"" }
@@ -1167,6 +1210,7 @@
   ,.request_url= "/music/sweet/music"
   ,.query_string= ""
   ,.fragment= ""
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers= { { "Host", "example.com" } }
   ,.body= ""
@@ -1187,6 +1231,7 @@
   ,.request_url= "/music/sweet/music"
   ,.query_string= ""
   ,.fragment= ""
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers= { { "Host", "example.com" } }
   ,.body= ""
@@ -1210,6 +1255,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Transfer-Encoding" , "deflate, chunked" }
@@ -1238,6 +1284,7 @@
   ,.fragment= ""
   ,.request_path= "/"
   ,.request_url= "/"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Transfer-Encoding" , "deflate, chunked" }
@@ -1275,6 +1322,7 @@
   ,.http_minor= 1
   ,.status_code= 301
   ,.response_status= "Moved Permanently"
+  ,.content_length= 219
   ,.num_headers= 8
   ,.headers=
     { { "Location", "http://www.google.com/" }
@@ -1324,6 +1372,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 5
   ,.headers=
     { { "Date", "Tue, 04 Aug 2009 07:59:32 GMT" }
@@ -1353,6 +1402,7 @@
   ,.http_minor= 1
   ,.status_code= 404
   ,.response_status= "Not Found"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= {}
   ,.body_size= 0
@@ -1368,6 +1418,7 @@
   ,.http_major= 1
   ,.http_minor= 1
   ,.status_code= 301
+  ,.content_length= -1
   ,.response_status= ""
   ,.num_headers= 0
   ,.headers= {}
@@ -1395,6 +1446,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.headers=
     { {"Content-Type", "text/plain" }
@@ -1422,6 +1474,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.headers=
     { {"Content-Type", "text/html; charset=utf-8" }
@@ -1446,6 +1499,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= 11
   ,.num_headers= 4
   ,.headers=
     { {"Content-Type", "text/html; charset=UTF-8" }
@@ -1472,6 +1526,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= 0
   ,.num_headers= 4
   ,.headers=
     { {"Server", "DCLK-AdSvr" }
@@ -1505,6 +1560,7 @@
   ,.http_minor= 0
   ,.status_code= 301
   ,.response_status= "Moved Permanently"
+  ,.content_length= 0
   ,.num_headers= 9
   ,.headers=
     { { "Date", "Thu, 03 Jun 2010 09:56:32 GMT" }
@@ -1544,6 +1600,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 11
   ,.headers=
     { { "Date", "Tue, 28 Sep 2010 01:14:13 GMT" }
@@ -1578,6 +1635,7 @@
   ,.http_minor= 1
   ,.status_code= 500
   ,.response_status= "Oriëntatieprobleem"
+  ,.content_length= 0
   ,.num_headers= 3
   ,.headers=
     { { "Date", "Fri, 5 Nov 2010 23:07:12 GMT+2" }
@@ -1599,6 +1657,7 @@
   ,.http_minor= 9
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers=
     {}
@@ -1622,6 +1681,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Content-Type", "text/plain" }
@@ -1641,6 +1701,7 @@
   ,.http_minor= 0
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Connection", "keep-alive" }
@@ -1661,6 +1722,7 @@
   ,.http_minor= 0
   ,.status_code= 204
   ,.response_status= "No content"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Connection", "keep-alive" }
@@ -1680,6 +1742,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers={}
   ,.body_size= 0
@@ -1697,6 +1760,7 @@
   ,.http_minor= 1
   ,.status_code= 204
   ,.response_status= "No content"
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers={}
   ,.body_size= 0
@@ -1715,6 +1779,7 @@
   ,.http_minor= 1
   ,.status_code= 204
   ,.response_status= "No content"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Connection", "close" }
@@ -1737,6 +1802,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers=
     { { "Transfer-Encoding", "chunked" }
@@ -1767,6 +1833,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= 16
   ,.num_headers= 7
   ,.headers=
     { { "Server",  "Microsoft-IIS/6.0" }
@@ -1805,6 +1872,7 @@
   ,.http_minor= 1
   ,.status_code= 301
   ,.response_status= "MovedPermanently"
+  ,.content_length= -1
   ,.num_headers= 9
   ,.headers= { { "Date", "Wed, 15 May 2013 17:06:33 GMT" }
              , { "Server", "Server" }
@@ -1832,6 +1900,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= ""
+  ,.content_length= -1
   ,.num_headers= 0
   ,.headers= {}
   ,.body= ""
@@ -1854,6 +1923,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.headers= { { "Content-Length-X", "0" }
              , { "Transfer-Encoding", "chunked" }
@@ -1878,6 +1948,7 @@
   ,.status_code= 101
   ,.response_status= "Switching Protocols"
   ,.upgrade= "proto"
+  ,.content_length= -1
   ,.num_headers= 2
   ,.headers=
     { { "Connection", "upgrade" }
@@ -1903,6 +1974,7 @@
   ,.response_status= "Switching Protocols"
   ,.body= "body"
   ,.upgrade= "proto"
+  ,.content_length= 4
   ,.num_headers= 3
   ,.headers=
     { { "Connection", "upgrade" }
@@ -1934,6 +2006,7 @@
   ,.response_status= "Switching Protocols"
   ,.body= "body"
   ,.upgrade= "proto"
+  ,.content_length= -1
   ,.num_headers= 3
   ,.headers=
     { { "Connection", "upgrade" }
@@ -1960,6 +2033,7 @@
   ,.response_status= "OK"
   ,.body= "body"
   ,.upgrade= NULL
+  ,.content_length= -1
   ,.num_headers= 2
   ,.headers=
     { { "Connection", "upgrade" }
@@ -1982,6 +2056,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= 4
   ,.num_headers= 3
   ,.body= "body"
   ,.upgrade= NULL
@@ -2012,6 +2087,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 3
   ,.body= "body"
   ,.upgrade= NULL
@@ -2039,6 +2115,7 @@
   ,.http_minor= 1
   ,.status_code= 200
   ,.response_status= "OK"
+  ,.content_length= -1
   ,.num_headers= 1
   ,.headers= { { "Transfer-Encoding", "chunked, identity" }
              }
@@ -2202,6 +2279,7 @@
   messages[num_messages].status_code = parser.status_code;
   messages[num_messages].http_major = parser.http_major;
   messages[num_messages].http_minor = parser.http_minor;
+  messages[num_messages].content_length = parser.content_length;
   messages[num_messages].headers_complete_cb_called = TRUE;
   messages[num_messages].should_keep_alive = http_should_keep_alive(&parser);
   return 0;
@@ -2650,6 +2728,7 @@
 
   MESSAGE_CHECK_NUM_EQ(expected, m, http_major);
   MESSAGE_CHECK_NUM_EQ(expected, m, http_minor);
+  MESSAGE_CHECK_NUM_EQ(expected, m, content_length);
 
   if (expected->type == HTTP_REQUEST) {
     MESSAGE_CHECK_NUM_EQ(expected, m, method);
@@ -4351,6 +4430,7 @@
       ,.http_minor= 0
       ,.status_code= 200
       ,.response_status= "OK"
+      ,.content_length= -1
       ,.num_headers= 2
       ,.headers=
         { { "Transfer-Encoding", "chunked" }
